//
//  MoreViewController.m
//  Music
//
//  Created by dllo on 16/6/7.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MoreViewController.h"
#import "LHQMoreDetailModel.h"
#import "LHQMoreDetailCollectionViewCell.h"
#import "RefreshViewController.h"
@interface MoreViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,retain)UICollectionView  *collectionView;
@property(nonatomic,retain)UICollectionViewFlowLayout  *flowLayout;
@property(nonatomic,retain)NSMutableArray  *dataSource;
@end

@implementation MoreViewController
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.dataSource = [NSMutableArray  array];
    }
    
    return self;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.tintColor = [UIColor  whiteColor];
    self.view.backgroundColor = [UIColor  whiteColor];
    [self  getDataSource];
}
-(void)collectionViewSubView
{
    
    self.flowLayout = [[UICollectionViewFlowLayout  alloc] init];
    self.flowLayout.itemSize = CGSizeMake((Screenwidth-24)/2-7, (Screenwidth-24)/2-7);
    self.flowLayout.scrollDirection =  UICollectionViewScrollDirectionVertical;
    //最小列间距
    self.flowLayout.minimumInteritemSpacing = 10 ;
    //最小行间距
    self.flowLayout.minimumLineSpacing = 10 ;
    //设置item据四边的间距
    self.flowLayout.sectionInset = UIEdgeInsetsMake(10 , 15, 10 , 15);
    self.collectionView = [[UICollectionView  alloc]initWithFrame:CGRectMake(0, 0, Screenwidth, Screenheight-64) collectionViewLayout:self.flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.bounces = NO;
    
    [self.collectionView  registerClass:[LHQMoreDetailCollectionViewCell  class] forCellWithReuseIdentifier:@"Cell"];
    [self.view  addSubview:_collectionView];

    
}
-(void)getDataSource
{
    NSString  *url = @"http://api.dongting.com/misc/album/new?page=1&size=30&app=ttpod&v=v9.0.2.2016052421&user_id=0&mid=iPhone8%2C2&f=f320&s=s310&imsi=&hid=&splus=9.3.2&active=1&net=2&openudid=714e0317ebb947064241a6abe30a1fc639430b24&idfa=F8C6B2DF-27B0-4A7B-9E83-A86B4F61321F&utdid=Vw2RqCpjWHwDAAGyCA35RHh7&alf=(null)&bundle_id=com.ttpod.music&latitude=38.88327473958334&longtitude=121.5445741102431&access_token=(null)&vc=9000200";
    [MyTools  getNetWithUrl:url body:nil header:nil response:LHQJSON success:^(id result) {
        NSDictionary  *Dict= (NSDictionary *)result;
        NSMutableArray  *TEMP = Dict[@"data"];
        for (NSDictionary  *dic in TEMP) {
            LHQMoreDetailModel  *model = [[ LHQMoreDetailModel  alloc]initWithDic:dic];
            [self.dataSource  addObject:model];
  
        }
        [self  collectionViewSubView];
        [self.collectionView  reloadData];
    
    } failure:^(NSError *error) {
        
    }];
    
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LHQMoreDetailCollectionViewCell  *cell = [collectionView  dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.model = [self.dataSource  objectAtIndex:indexPath.item];
    
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((Screenwidth-40)/2,(Screenwidth-40)/2+40);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    RefreshViewController  *refresh = [[RefreshViewController  alloc] init];
    LHQMoreDetailModel  *model  = self.dataSource[indexPath.item];
    NSString  *url = model.id;
    refresh.refreshCount = [url  integerValue];
    [self.navigationController  pushViewController:refresh animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
